
<template>
  <el-card shadow="hover">
    <el-table
        :data="studentList"
        style="width: 100%">
      <el-table-column
          label="学号"
          align="center"
          prop="sid"
          width="180"/>
      <el-table-column
          label="姓名"
          align="center"
          prop="sname"/>
      <el-table-column
          label="性别"
          align="center"
          prop="sex"/>
      <el-table-column
          label="年龄"
          align="center"
          prop="age"/>
      <el-table-column
          label="住址"
          align="center"
          prop="addr"/>
      <el-table-column
          label="所在班级"
          align="center"
          prop="cname"/>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页导航-->
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>
  </el-card>

</template>

<script>
export default {
  data() {
    return {
      studentList: [],
      page: 1,            // 当前页码
      pageSize: 5,        // 每页显示条数
      total: 100          // 总记录数
    }
  },
  created() {
    // 1. 查询所有学生
    // this.findStudents()
    // 2. 分页查询学生
    this.findByPage()
  },
  methods: {
    findByPage() {
      this.$axios.get(`/student/findByPage/${this.page}/${this.pageSize}`).then(({data}) => {
        this.studentList = data.rows
        this.total = data.total
      })
    },
    // 页大小pagesize改变时的事件：
    handleSizeChange(val) {
      this.pageSize = val
      this.findByPage()
      console.log(`每页 ${val} 条`);
    },
    // 页码改变时的事件：
    handleCurrentChange(val) {
      this.page = val
      this.findByPage()
      console.log(`当前页: ${val}`);
    },
    findStudents(){
      this.$axios.get("/student/list").then( ({data}) => {
        this.studentList = data
      })
    },
    handleEdit(index, row) {
      console.log(index, row);
    },
    handleDelete(index, row) {
      console.log(index, row);
    }
  }
}
</script>

<style scoped>
  .el-pagination {
    text-align: right;
    margin-top: 20px;
  }
</style>
